<?php

namespace app\common\widget;

class Form extends \think\Controller
{

    private $widgetPath;

    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->widgetPath = APP_PATH . 'common/widget/form/';
    }

    /**
     * 代码控件
     */
    public function code($field, $value = '')
    {
        $parse = <<<HTML
        <textarea id="{$field}" name="{$field}">{$value}</textarea>
<script>
In('codemirror',function(){
    var {$field} = CodeMirror.fromTextArea(document.getElementById("{$field}"), 
    {
        lineNumbers: true,
        matchBrackets: true,
        mode: "application/x-httpd-php",
        indentUnit: 4,
        indentWithTabs: true,
        styleActiveLine: true,
        onChange: function (n){
            $('#{$field}').val({$field}.getValue());
            console.info({$field}.getValue());
        }
    });
})

</script>
HTML;
        return $parse;
    }

    /**
     * 富文本控件
     */
    public function ueditor($field, $value = '', $uid=0)
    {
        $parse = <<<HTML
<script id="{$field}" name="{$field}" type="text/plain">{$value}</script>
<script>
In('ueditor',function(){
    var {$field}Editor = UE.getEditor('{$field}',{
        serverUrl: '/plus/uploader/index.html?uid={$uid}'
    });
})
</script>
HTML;
        return $parse;
    }

    /**
     * 单图上传控件
     */
    public function singleImg($field, $value = '', $uid=0)
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'singleImg.html');
    }

    /**
     * 多图上传控件
     */
    public function moreImg($field, $value = '', $uid=0)
    {
        if (!empty($value)) {
            $imgList = explode('|', $value);
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $imgList);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'moreImg.html');
    }

    /**
     * 多图上传控件,包含标题和描述
     */
    public function moreTextImg($field, $value = '', $tips = '请输入图片标题', $uid=0)
    {
        $arr = [];
        if (!empty($value) && $value !== '######') {
            $list      = explode('###', $value);
            $imgList   = explode('|', $list[0]); //图片
            $titleList = explode('|', $list[1]); //标题
            $infoList  = explode('|', $list[2]); //描述
            //图片、文字、备注
            foreach ($imgList as $k => $v) {
                $arr[$k]['imgurl'] = $v;
                $arr[$k]['title']  = $titleList[$k];
                $arr[$k]['info']   = $infoList[$k];
            }
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $arr);
        $this->assign('uid', $uid);
        $this->assign('tips', $tips);
        return $this->fetch($this->widgetPath . 'moreTextImg.html');
    }

    /**
     * 附件上传控件
     */
    public function singleFile($field, $value = '', $uid=0)
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('uid', $uid);
        return $this->fetch($this->widgetPath . 'singleFile.html');
    }
    /**
     * 扩展属性控件
     * @param $field
     * @param $value
     * @return mixed
     */
    public function extend($field, $value=[])
    {
        $value = empty($value)?[]:$value;
        $extendStr = '';
        foreach ($value as $k=>$v){
            $extendStr .= "{$k}:{$v}\r\n";
        }
        $this->assign('field', $field);
        $this->assign('extendStr', $extendStr);
        $content = $this->fetch($this->widgetPath . 'extend.html');
        return $content;
    }

    /**
     * 省市区级联
     * @param array $fields 级联字段名数组
     * @param array $value 级联值数组
     * @return mixed
     */
    public function areaName($field = 'prov_id,city_id,dist_id', $value = '0,0,0')
    {
        $this->assign('fields', explode(',', $field));
        $this->assign('value', explode(',', $value));
        return $this->fetch($this->widgetPath . 'areaName.html');
    }

}